
import { createRouter, createWebHistory } from 'vue-router'
// 懒加载-使得打包模块化-静态请求时只请求对应模块的静态数据-更为快速
const Home = () => import('@/views/home')
const Category = () => import('@/views/category')
const ShopCar = () => import('@/views/shopcar')
const Profile = () => import('@/views/profile')


// 1.加载
// vue2方式：Vue.use(VueRouter)
// vue3方式
const routerHistory = createWebHistory()

const routes = [
    {
        path: '',
        redirect: '/home',
        meta: {
            title: '首页',

        }
    },
    {
        path: '/home',
        component: Home,
        meta: {
            title: '首页'
        }
    },
    {
        path: '/category',
        component: Category,
        meta: {
            title: '分类'
        }
    },
    {
        path: '/shopcar',
        component: ShopCar,
        meta: {
            title: '购物车',
            reqToken: true
        }
    },
    {
        path: '/profile',
        component: Profile,
        meta: {
            title: '我的'
        }
    }

]

// 2.创建
const routers = createRouter({
    routes,
    history: routerHistory
})

// 3.导航守卫
routers.beforeEach((to, form, next) => {
    document.title = to.matched[0].meta.title
    // let token = window.localStorage.getItem('token')
    // if (to.matched[0].meta.reqToken && !token) {
    //     next({
    //         path: '',
    //         query: {
    //             redirect: to.fullPath
    //         }
    //     })
    // }
    next()
})


// 4.导出

export default routers